package com.wechat.feign;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fhMall.response.ResponseData;
import com.wechat.core.constant.enums.support.RStatus;
import com.wechat.core.exception.ServiceException;
import com.wechat.models.response.FeignResult;

public interface FeignBaseService {

    default <T extends ResponseData> T toResponse(String json, TypeReference<FeignResult<T>> typeReference ){
        FeignResult<T> feignResult = JSONObject.parseObject(json,typeReference);
        if(feignResult == null){
            throw new ServiceException("系统错误");
        }
        if (!feignResult.isSuccess() || feignResult.getCode() != RStatus.SUCCESS.getValue()) {
            throw new ServiceException(feignResult.getMessage());
        }
        return feignResult.getResult();
    }
}
